<!DOCTYPE html>
<html lang="en">

<head>
    <% include ../components/head %>
    <style>
        .mt-20 {
            margin-top: 20px;
        }
        textarea {
            background: #ededed !important;
            resize: none;
        }
    </style>
</head>

<body>

<main class="container" style="margin-bottom: 50px">
    <div class="col-md-6 offset-md-3 col-sm-12">
        <h5><b>Together to fight the Pandemic!</b></h5>
        <h5>Do you encounter any difficulties? Please let us know and we will try our best to help you!</h5>
        <section class="mt-20 form-group">
            <h5><b>Your difficulties</b></h5>
            <textarea class="form-control difficulties" rows="5"></textarea>
        </section>
        <section class="mt-20 form-group">
            <h5><b>What help do you wish to get?</b></h5>
            <textarea class="form-control help" rows="5"></textarea>
        </section>
        <section class="mt-20 text-center">
            <button type="button" class="btn btn-primary border-0" id="submit">Submit</button>
        </section>

    </div>
</main>
<script
        src="https://code.jquery.com/jquery-3.4.1.min.js"
        integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo="
        crossorigin="anonymous"></script>
<script>
    $(function () {
        $('#submit').click(function () {
            if ($('.difficulties').val().length === 0) {
                alert('Please enter your problems');
                return;
            }
            if ($('.help').val().length === 0) {
                alert('Please enter your requirements');
                return;
            }
            $.ajax({
                url: "https://dc-api.rupee-mama.in" + "/user/assistance/submit",
                type: "POST",
                headers: {
                    basicParams: decodeURI(getQueryVariable('basicParams'))
                },
                data: JSON.stringify({
                    difficulties: $('.difficulties').val(),
                    helpContent: $('.help').val()
                }),
                dataType: 'json',
                contentType: 'application/json;charset=utf-8',
                success: function (res) {
                    setCookie('d', $('.difficulties').val(), 1);
                    setCookie('help', $('.help').val(), 1);
                    window.location.assign(window.location.origin +'/assistance-recorded');
                    console.log(res);
                },
                error: function (res) {
                    console.log(res);
                }
            })
        });
    });
    function getQueryVariable(variable) {
        var query = window.location.search.substring(1);
        var vars = query.split("&");
        for (var i = 0; i < vars.length; i++) {
            var pair = vars[i].split("=");
            if (pair[0] == variable) {
                console.log(decodeURI(pair[1]));
                return pair[1];
            }
        }
        return (false);
    }
    function setCookie(cname, cvalue, exdays) {
        var d = new Date();
        d.setTime(d.getTime() + (exdays*24*60*60*1000));
        var expires = "expires="+ d.toUTCString();
        document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
    }
</script>
</body>

</html>
